package com.ai.platform.core.service.impl;


import com.ai.platform.core.context.InvocationContextHolder;
import com.ai.platform.model.dto.Input;
import com.ai.platform.model.dto.Output;
import com.ai.platform.core.rpc.ApiService;
import com.ai.platform.core.service.IControlService;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yeyingsheng on 2018/4/30.
 */
public class ControlServiceImpl implements IControlService {

    private ApiService apiService;

    public void setApiService(ApiService apiService) {
        this.apiService = apiService;
    }

    @Override
    public Output execute(Input input) {
        return this.execute(input,false);
    }

    @Override
    public Output execute(String serviceName, String method, String[]... params) {
        return this.execute(serviceName,method,false,params);
    }

    @Override
    public Output execute(String serviceName, String method, String... params) {
        return this.execute(serviceName,method,false,params);
    }

    @Override
    public Output execute(String serviceName, String method, Map<String, String[]> paramMap) {
        return this.execute(serviceName,method,false,paramMap);
    }

    @Override
    public Output execute(Input input,boolean isOverride) {
        if(!isOverride){
            InvocationContextHolder contextHolder=InvocationContextHolder.getContext();
            Input in=contextHolder.getInput();
            if(StringUtils.isNotBlank(input.getServiceName())){
                in.setServiceName(input.getServiceName());
            }
            if(StringUtils.isNotBlank(input.getMethod())){
                in.setMethod(input.getMethod());
            }
            if(StringUtils.isNotBlank(input.getIp())){
                in.setIp(input.getIp());
            }
            in.getInputMap().putAll(input.getInputMap());
            input=in;
        }
        return apiService.execute(input);
    }

    @Override
    public Output execute(String serviceName, String method,boolean isOverride, String[]... params) {
        Map<String,String[]> paramMap=new HashMap<>();

        if(null!=params && params.length>0){
            for(String[] strings:params){
                if(strings.length==2) {
                    String[] values=new String[]{strings[1]};
                    paramMap.put(strings[0], values);
                }
            }
        }
        Input input=new Input(serviceName,method,paramMap);
        return this.execute(input,isOverride);
    }

    @Override
    public Output execute(String serviceName, String method,boolean isOverride, String... params) {
        Map<String,String[]> paramMap=new HashMap<>();
        if(null!=params && params.length>0){
            //以两个参数组成一个K,V
            for(int i=0;i<params.length;i=i+2){
                String[] values=new String[]{params[i+1]};
                paramMap.put(params[i],(i+1>=params.length)?null:values);
            }
        }
        Input input=new Input(serviceName,method,paramMap);
        return this.execute(input,isOverride);
    }

    @Override
    public Output execute(String serviceName, String method,boolean isOverride, Map<String, String[]> paramMap) {
        Input input=new Input(serviceName,method,paramMap);
        return this.execute(input,isOverride);
    }

}
